package com.jinxin.platform.cdfaceacs.api;

import com.github.pagehelper.PageInfo;
import com.jinxin.platform.base.common.pojo.JsonResult;

import com.jinxin.platform.cdfaceacs.common.ResultEntity;
import com.jinxin.platform.cdfaceacs.pojo.domains.AcsControlRecordInfo;
import com.jinxin.platform.cdfaceacs.service.AcsControlModuleRegService;
import com.jinxin.platform.cdfaceacs.service.AcsControlRecordInfoService;
import com.jinxin.platform.cdfaceacs.service.AcsControlUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * Description: apcos-acs
 * Created by 34759 on 2020/4/26 15:20
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/unAuth/app/userinfo")
public class AcsUserInferInterface {

    @Autowired
    private AcsControlUserInfoService userInfoService;
    @Autowired
    private AcsControlModuleRegService regService;
    @Resource
    private AcsControlRecordInfoService acsControlRecordInfoService;



    /**
     * 提交邀请访客记录
     * @param operatorId 业主ACS UID
     * @param operatorName 业主名字
     * @param visitorName 访客名字
     * @param recordInfo 验证信息 随机数字 随机二维码(字符串)
     * @param type 类型 3随机二维码 4随机数字 5人脸
     * @param listenedTime 邀请有效截止时间
     * @param file 访客人脸图片
     * @return
     */
    @PostMapping("/addAcsCodeLog")
    public ResultEntity addAcsCodeLog(String operatorId, String operatorName,
                                      String visitorName, String recordInfo, String type, String listenedTime, MultipartFile file ){
        HashMap<Object, Object> hashMap = new HashMap();
        if(!StringUtils.isEmpty(operatorId)){
            hashMap.put("operatorId",operatorId );
        }
        if(!StringUtils.isEmpty(operatorName)){
            hashMap.put("operatorName",operatorName );
        }
        if(!StringUtils.isEmpty(visitorName)){
            hashMap.put("visitorName",visitorName );
        }
        if(!StringUtils.isEmpty(recordInfo)){
            hashMap.put("recordInfo",recordInfo );
        }
        if(!StringUtils.isEmpty(listenedTime)){
            hashMap.put("listenedTime",listenedTime );
        }
//        if(null != file ){
//            hashMap.put("file", file);
//        }
        hashMap.put("createTime", LocalDateTime.now());
        // 二维码信息和随机数字 保存在 recordInfo 字段，人脸信息单独保存在图片base64表中后，recordInfo字段保存图片ID
        JsonResult jsonResult = new JsonResult();
        if(StringUtils.isEmpty(type)|| type.equals("3") ){
            jsonResult = regService.addAcsVisCodeLogInfo(hashMap);
        }
        if( type.equals("4") ){
            jsonResult =  regService.addAcsVisNumLogInfo(hashMap);
        }
        if( type.equals("5") ){
            if( null == file  ){
                return new ResultEntity("2", "人脸信息不能为空","" );
            }
            jsonResult =  regService.addAcsVisFaceLogInfo(hashMap,file);
        }
        if(jsonResult.getSuccess() ){
            return new ResultEntity("1","添加成功","" );
        }else{
            return new ResultEntity("2",jsonResult.getMsg(),"" );
        }
    }


    @PostMapping("/getTable")
    public ResultEntity getTableInfo(int page, int limit, String recordType,String deviceId,String listenType,String state ){
        if(StringUtils.isEmpty(recordType) ){
            return new ResultEntity("2","查询类型为空","" );
        }
        // APP查询通话记录 type 2   listenType(0全部 1未接通)  根据listenedTime 不为空判断
        // APP查询访客记录 type 6 state(1未使用 2已使用) 人脸应该判断不到
        Map<String,Object> resultMap = new HashMap<>();
        PageInfo<AcsControlRecordInfo> recordInfoPageInfo;
        if(recordType.equals("2") ){
            recordInfoPageInfo = acsControlRecordInfoService.queryPageByLimitApp(page,limit,deviceId,listenType);
        }else{
            recordInfoPageInfo = acsControlRecordInfoService.queryInvitePageByLimitApp(page,limit,state,deviceId);
        }
        resultMap.put("code","0");
        resultMap.put("msg","");
        resultMap.put("count",recordInfoPageInfo.getTotal());
        resultMap.put("data",recordInfoPageInfo.getList());
        return new ResultEntity("1","查询成功",resultMap );
    }

}
